package com.example.chessbattleplatform.web.room_board.dataService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chessbattleplatform.web.room_board.Pojo.mapper.BoardDataMapper;
import com.example.chessbattleplatform.web.room_board.dataService.impl.RoomBoardDataServiceInterface;
import com.example.chessbattleplatform.web.room_board.mapper.RoomBoardMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class RoomBoardDataService extends ServiceImpl<RoomBoardMapper, BoardDataMapper> implements RoomBoardDataServiceInterface {


    private final RoomBoardMapper roomBoardMapper;

    //添加房间
    @Override
    public boolean addRoom(String idRooms, String userIdBlack, String userIdWhite, String boardData) {
        BoardDataMapper boardDataMapper = new BoardDataMapper(idRooms, userIdBlack, userIdWhite, boardData, null, null);
        int insert = roomBoardMapper.insert(boardDataMapper);
        return insert > 0;
    }

    //根据 用户 id 查询查询房间
    @Override
    public BoardDataMapper getRoom(String userId) {
        LambdaQueryWrapper<BoardDataMapper> lambdaQueryWrapper = new LambdaQueryWrapper<>(BoardDataMapper.class)
                .allEq(Map.of(BoardDataMapper::getUserIdWhite, userId, BoardDataMapper::getUserIdBlack, userId));

        return roomBoardMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public BoardDataMapper updateRoom(Long userId) {
        return null;
    }

    // 根据 房间id 删除房间
    @Override
    public boolean removeById(String idRooms) {
        LambdaQueryWrapper<BoardDataMapper> lambdaQueryWrapper = new LambdaQueryWrapper<>(BoardDataMapper.class)
                .eq(BoardDataMapper::getIdRooms, idRooms);
        return roomBoardMapper.delete(lambdaQueryWrapper) > 0;
    }


}
